pacman::p_load(QCA, cna, readxl, tidyverse)
options(digits = 2)

# Abertura comercial, todas observacoes
foo <- read_xlsx("dados_(todas_rodadas).xlsx", sheet = 1) %>%
  column_to_rownames("pais")
n <- names(foo) 

# Concentracao de mercado, perde Franca 92, Luxemburgo 92 e Uruguai 91
bar <- read_xlsx("dados_(todas_rodadas).xlsx", sheet = 2) %>%
  column_to_rownames("pais")
n2 <- names(bar) 
#

# Calibracao (ja realizada) ####
# Crescimento
foo$crescimento <- calibrate(foo$PIB4anos, type = "fuzzy",
                            thresholds = "e=0, c=2, i=4")

bar$crescimento <- calibrate(bar$PIB4anos, type = "fuzzy",
                            thresholds = "e=0, c=2, i=4")

# Fragmentacao
foo$frag <- calibrate(foo$nep, type = "fuzzy",
                      thresholds = "e=3, c=4.2, i=6")
bar$frag <- calibrate(bar$nep, type = "fuzzy",
                      thresholds = "e=3, c=4.2, i=6")

# Abertura Comercial
tfs <- findTh(foo$abertura_comercial, groups = 4)
foo$abertura_com_cali <- calibrate(foo$abertura_comercial, type = "fuzzy",
                                  thresholds = tfs, logistic = F)

# Concentracao de Mercado
tfs <- findTh(bar$conc_mercado, groups = 4)
bar$conc_mercado_cali <- calibrate(bar$conc_mercado, type = "fuzzy",
                                  thresholds = tfs, logistic = F)


#
# Sem condicao economica ####
superSubset(foo, outcome = "par_legis",
            conditions = c("democ_pres", "bicame", "deb.prev", "frag"),
            incl.cut = .9)

tt1 <- truthTable(foo, outcome = "par_legis",
                 conditions = c("democ_pres", "bicame", "deb.prev", "frag"),
                 incl.cut = .70, complete = T, show.cases = T,
                 sort.by = c("incl", "n"))

minimize(tt1, details = T, show.cases = T, include = "?")

#
# Com crescimento ####
superSubset(foo, outcome = "par_legis",
            conditions = c("democ_pres", "bicame", "deb.prev", "frag", "crescimento"),
            incl.cut = .9)

tt2 <- truthTable(foo, outcome = "par_legis",
                  conditions = c("democ_pres", "bicame", "deb.prev", "frag", "crescimento"),
                  incl.cut = .8, complete = T, show.cases = T,
                  sort.by = c("incl", "n"))

minimize(tt2, details = T, show.cases = T, use.tilde = F)

#
# Com abertura comercial ####
superSubset(foo, outcome = "par_legis",
            conditions = c("democ_pres", "bicame", "deb.prev", "frag", "abertura_comer"),
            incl.cut = .9)

tt3 <- truthTable(foo, outcome = "par_legis",
                  conditions = c("democ_pres", "bicame", "deb.prev", "frag", "abertura_comer"),
                  incl.cut = .8, complete = T, show.cases = T,
                  sort.by = c("incl", "n"))

minimize(tt3, details = T, show.cases = T, use.tilde = F)

#
# Com concentracao de mercado ####
superSubset(bar, outcome = "par_legis",
            conditions = c("democ_pres", "bicame", "deb.prev", "frag", "conc_merc"),
            incl.cut = .9, ron.cut = .3)

tt4 <- truthTable(bar, outcome = "par_legis",
                  conditions = c("democ_pres", "bicame", "deb.prev", "frag", "conc_merc"),
                  incl.cut = .8, complete = T, show.cases = T,
                  sort.by = c("incl", "n"))

minimize(tt4, details = T, show.cases = T, use.tilde = F)

#

# CNA ####
# Sem condicao economica
fscna(foo[, c(1, 2, 3, 4, 8)],
      ordering = list(c("democ_pres", "bicame", "frag"),
                      c("deb.prev"), "par_legis"),
      con = .75, cov = .75, strict = T, details = T, what = "m, a, c")

print(
  condition("democ_pres*BICAME + FRAG*CRESCIMENTO <-> DEB.PREV",
            selectCases1("democ_pres*BICAME + FRAG*CRESCIMENTO <-> DEB.PREV",
                         con = .65, cov = .65,
                         type = "fs", foo[, c(1, 2, 3, 4, 8, 10)])),
  show.cases = T)

# Com crescimento
fscna(foo[, c(1, 2, 3, 4, 8, 10)],
      ordering = list(c("democ_pres", "bicame", "frag"),
                      "crescimento", "deb.prev", "par_legis"),
      con = .75, cov = .75, strict = T, details = T, what = "m, a, c")

# Com abertura comercial
fscna(foo[, c(1, 2, 3, 4, 8, 12)],
      ordering = list(c("democ_pres", "bicame", "frag"),
                      "abertura_comer", "deb.prev", "par_legis"),
      con = .75, cov = .75, strict = T, details = T, what = "m, a, c")

# Com concentracao de mercado
fscna(bar[, c(1, 2, 3, 4, 8, 12)],
      ordering = list(c("democ_pres", "bicame", "frag"),
                      "conc_merc", "deb.prev", "par_legis"),
      con = .75, cov = .75, strict = T, details = T, what = "m, a, c")

print(
  condition("democ_pres*BICAME + democ_pres*FRAG*conc_merc <-> DEB.PREV",
            selectCases1("democ_pres*BICAME + democ_pres*FRAG*conc_merc <-> DEB.PREV", con = .75, cov = .75,
                         type = "fs", bar[, c(1, 2, 3, 4, 8, 12)])),
  show.cases = T)
